package org.databasefacade.update;

import org.databasefacade.select.Arithmetic;
import org.databasefacade.statements.CaseStatement;
import org.databasefacade.statements.SelectStatement;
import org.databasefacade.table.column.Column;

public class UpdateSet {
	public static final int COLUMN_SOURCE=1;
	public static final int ARITHMETIC_SOURCE=2;
	public static final int SELECT_STATEMENT_SOURCE=3;
	public static final int CASE_STATEMENT_SOURCE=4;
	public static final int VALUE_SOURCE=5;
	
	private Column target;
	private Object source;
	private int sourceType;
	
	public UpdateSet(Column target, Column source) {
		this.target = target;
		this.source = source;
		sourceType = COLUMN_SOURCE;
	}
	
	public UpdateSet(Column target, Arithmetic calc) {
		this.target = target;
		this.source = calc;
		sourceType = ARITHMETIC_SOURCE;
	}
	
	public UpdateSet(Column target, SelectStatement selectStmt) {
		this.target = target;
		this.source = selectStmt;
		sourceType = SELECT_STATEMENT_SOURCE;
	}
	
	public UpdateSet(Column target, CaseStatement caseStmt) {
		this.target = target;
		this.source = caseStmt;
		sourceType = CASE_STATEMENT_SOURCE;
	}
	
	public UpdateSet(Column target, Object value) {
		this.target = target;
		this.source = value;
		sourceType = VALUE_SOURCE;
	}
	
	public int getSetSourceType() {
		return sourceType;
	}
	
	public Column getSetTarget() {
		return target;
	}
	
	public Object getSetSource() {
		return source;
	}
	
	public String getSourceSQL() {
		if ( sourceType == COLUMN_SOURCE ) {
			Column col = (Column) source;
			return col.getTableAlias() + "." + col.getColumnName();
		} else if ( sourceType == ARITHMETIC_SOURCE ) {
			Arithmetic calc = (Arithmetic) source;
			return calc.getSQL();
		} else if ( sourceType == SELECT_STATEMENT_SOURCE ) {
			SelectStatement selectStmt = (SelectStatement) source;
			return selectStmt.getSQL();
		} else if ( sourceType == CASE_STATEMENT_SOURCE ) {
			CaseStatement caseStmt = (CaseStatement) source;
			return caseStmt.getSQL();
		} else if ( sourceType == VALUE_SOURCE ) {
			return source.toString();
		}
		return null;
	}
}
